package other.thread;

import java.util.concurrent.*;

public class ThreadTest {

    public static void main(String[] args) {
        ThreadTest threadTest = new ThreadTest();
//        threadTest.testInterrupt();
        System.out.println("main线程名："+Thread.currentThread());
//        threadTest.testCallable();
        threadTest.threadPool();

    }

    private void testCreateThread() {
        final int[] num = {0};
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                synchronized (num){
                    num[0]++;
                    System.out.println("线程1得到："+num[0]);
                }
//                try {
//                    Thread.sleep(300);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
            }
        }, "thread1");
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 4; i++) {
                synchronized (num) {
                    num[0]++;
                    System.out.println("线程2得到："+num[0]);
                }
//                try {
//                    Thread.sleep(300);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
            }
        }, "thread2");
        t1.setPriority(5);//设置优先级，默认为5
        t2.setPriority(5);
//        t1.start();
//        t2.start();
//        System.out.println(t1.getName());
    }

    private void testRunnable() {
        MyRunnable myRunnable = new MyRunnable(); //实现Runnable接口，里面只包含一个run方法，没有start方法
        //创建实现Runnable接口的线程，他们是不同的线程，但是用的是同一个MyRunnable实现类，所以两个线程用的是相同的属性
        Thread thread01 = new Thread(myRunnable);
        Thread thread02 = new Thread(myRunnable);

        //检验hashcode是否相同，检验结果为不同，这是因为是不同的线程
//        System.out.println(thread01.hashCode());
//        System.out.println(thread02.hashCode());

        thread01.start();
        thread02.start();
    }

    private void testInterrupt() {
        MyThread myThread = new MyThread();

        myThread.start();//启动线程
        for (int i = 0; i < 5; i++) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        myThread.interrupt();//查看线程是否受阻，如果是，抛出相关异常，得以退出阻塞状态
        System.out.println(myThread.isInterrupted());//判断是否处于阻塞状态
    }

    private void testCallable() {
        MyCallable myCallable = new MyCallable();
        FutureTask futureTask = new FutureTask<>(myCallable);
        Thread thread = new Thread(futureTask);
        thread.start();

        try {
            thread.join();
            Object o = futureTask.get();
            System.out.println("得到线程返回值："+o);
        } catch (InterruptedException e) {
            System.out.println("线程正在等待其他线程执行完毕，请等候");
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }

    private void threadPool() {
        //1. 提供指定线程数量的线程池
        ExecutorService service = Executors.newFixedThreadPool(10);
        ThreadPoolExecutor service1 = (ThreadPoolExecutor) service;
        //设置线程池的属性
        //System.out.println(service.getClass());
        //service1.setCorePoolSize(15);
        //service1.setKeepAliveTime();

        MyRunnable myRunnable = new MyRunnable();
        Thread thread = new Thread(myRunnable);

        //2.执行指定的线程的操作。需要提供实现Runnable接口或Callable接口实现类的对象
        service.execute(thread);//适合适用于Runnable
        service.execute(thread);//适合适用于Runnable

        //service.submit(Callable callable);//适合使用于Callable
        //3.关闭连接池
        service.shutdown();
    }
}
